 /**
 * 逻辑、功能相关描述: <br>
 *
 * @author 付湘辉  <br>
 * 编写日期：2008-12-24  <br>
 */
package com.ce.pms.charge.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.util.Assert;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.charge.dao.IChargeRecordDao;
import com.ce.pms.charge.model.ChargeRecordDO;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.common.util.StringAnalysiUtil;
import com.ce.util.BlankUtil;


/**
 * 	名称: com.ce.pms.charge.dao.impl.ChargeRecordDaoImpl <br>
 *  非常规费用记录Dao实现
 *
 * @author  付湘辉(开发) ，马思权(设计)  <br>
 * Created on： 2008-12-24 <br>
 * @author <br>
 * update on： <br>
 * why: <br>
 */
public class ChargeRecordDaoImpl extends PmsBaseDaoImpl<ChargeRecordDO> implements
		IChargeRecordDao {
	/**
	 * 功能描述:根据多个房屋ID查询费用记录  <br>
	 * @param houseIds 房屋ID逗号拼接字符串 如：1,2,3
	 * @return List<ChargeRecordDO>
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-12-24  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@SuppressWarnings("unchecked")
	public List<ChargeRecordDO> getUnHandleChargeRecords(String houseIds){
		if (BlankUtil.isBlank(houseIds)) {
			return new ArrayList<ChargeRecordDO>();
		}
		String hql = "from ChargeRecordDO crd where crd.house.id in (:houseIdStr) and crd.handleYn=0 ";
		hql = hql.replace(":houseIdStr", houseIds);
		Query  query = super.createQuery(hql);
		List<ChargeRecordDO>  list= query.list();
		return list;
	}

	/**
	 * 功能描述:删除未处理的费用记录<br>
	 * @param bizModule
	 * @param bizType
	 * @param bizId<br>
	 * Create author:罗军林<br>
	 * Create on:2008-12-25<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public Boolean deleteChargeRecordByBiz(String bizModule,String bizType,Long bizId){
		String hql="DELETE FROM FEE_CHARGE_RECORD WHERE BIZ_MOUDLE=? AND BIZ_TYPE=? AND BIZ_ID=? AND HANDLE_YN='0' ";
		Query query=createSQLQuery(hql, bizModule,bizType,bizId);
		int result=query.executeUpdate();
		return result>0;
	}

	/**
	 * 功能描述:更新费用记录的处理状态为已处理  <br>
	 * @param chargeRecordIds 费用记录ID集
	 * @param operator 操作者ID
	 * @param modifyDate 修改日期
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-12-31  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 * @param operator TODO
	 * @param modifyDate TODO
	 */
	public void updateChargeRecordHandleStatusToYes(List<Long> chargeRecordIds, Long operator, Date modifyDate){
		String sql="UPDATE FEE_CHARGE_RECORD SET HANDLE_YN=1,MODIFIER=?,MODIFY_DATE=? WHERE CHARGE_RECORD_ID=? ";
		List<Object[]> parameter=new ArrayList<Object[]>();
		for(Long chargeRecordId : chargeRecordIds){
			parameter.add(new Object[]{operator,modifyDate,chargeRecordId});
		}
		synchronized (this) {
			executeSQLBatch(sql, parameter);
		}
	}

	/**
	 * 功能描述: 查询指定业务模块，类型，ID的费用记录 <br>
	 * @param bizModule
	 * @param bizType
	 * @param bizId
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2009-1-5  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@SuppressWarnings("unchecked")
	public ChargeRecordDO getChargeRecordByBiz(String bizModule,String bizType,Long bizId){
		String hql="from ChargeRecordDO crd where crd.bizModule=? and crd.bizType=? and crd.bizId=? ";
		Query query=createQuery(hql, bizModule,bizType,bizId);
		List<ChargeRecordDO> list=query.list();
		return list.size()==0?null:list.get(0);
	}

	/**
	 * 功能描述: 查询指定业务模块，业务类型，业务ID的费用记录 <br>
	 * @param bizModule 业务模块
	 * @param bizType 业务类型
	 * @param bizIds 业务ID接接而成的一个字符串，以逗号分隔
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2009-1-5  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@SuppressWarnings("unchecked")
	public List<ChargeRecordDO> queryChargeRecordList(String bizModule,String bizType,String bizIdString){
		String hql="from ChargeRecordDO crd where crd.bizModule=:bizModule and crd.bizType=:bizType and crd.bizId in (:idString) ";
		hql = hql.replace(":idString",bizIdString);
		Map<String,Object> parameter = new HashMap<String, Object>();
		parameter.put("bizModule", bizModule);
		parameter.put("bizType", bizType);
		Query query=super.createNameQuery(hql, parameter);
		List<ChargeRecordDO> list=query.list();
		return list;
	}

	/**
	 * 功能描述: 检查某项费用的所有未入账费用记录是否都已经处理 <br>
	 * @param bizModule 业务模块
	 * @param bizType 业务类型
	 * @param bizId 业务ID
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2009-1-7  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public  Boolean  checkFinishHandleYn (String bizModule, String bizType, List<Long> bizIds){
		Assert.isTrue(!BlankUtil.isBlank(bizModule));
		Assert.isTrue(!BlankUtil.isBlank(bizType));
		Assert.isTrue(!BlankUtil.isBlank(bizIds));
		String bizIdString = StringAnalysiUtil.portfolioFormattedString(bizIds);
		List<ChargeRecordDO> list = this.queryChargeRecordList(bizModule, bizType, bizIdString);
		for (ChargeRecordDO recordDO : list) {
			if (SysrefcodeDO.CONSTANTS_DIGITAL_ZERO.equals(recordDO.getHandleYn())) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 功能描述: 检查某项费用的所有未入账费用记录是否都未处理 <br>
	 * 只要有一项已经处理，都将返回false
	 * @param bizModule 业务模块
	 * @param bizType 业务类型
	 * @param bizId 业务ID
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2009-1-7  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public  Boolean  checkFinishIsHandle (String bizModule, String bizType, List<Long> bizIds){
		Assert.isTrue(!BlankUtil.isBlank(bizModule));
		Assert.isTrue(!BlankUtil.isBlank(bizType));
		Assert.isTrue(!BlankUtil.isBlank(bizIds));
		String bizIdString = StringAnalysiUtil.portfolioFormattedString(bizIds);
		List<ChargeRecordDO> list = this.queryChargeRecordList(bizModule, bizType, bizIdString);
		for (ChargeRecordDO recordDO : list) {
			if (SysrefcodeDO.CONSTANTS_DIGITAL_ONE.equals(recordDO.getHandleYn())) {
				return false;
			}
		}
		return true;
	}
}
